package com.seio.edc.controller.socialBenefits;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.energy.OverallStatisticsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.energy.Energy;
import com.nova.edc.model.system.ElecEnergy;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.EnergyService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.ElecEnergyService;
import com.seio.edc.controller.core.BaseContoller;
import com.seio.edc.util.DateTimeUtil;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.StringUtil;

@Controller
@RequestMapping(value = "/socialBenefits")
public class SocialBenefitsContronller extends BaseContoller {

    private static Logger log = Logger
            .getLogger(SocialBenefitsContronller.class);

    @Autowired
    private EnergyService novaEnergyService;

    @Autowired
    private ElecEnergyService novaElecEnergyService;

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    @Autowired
    private OverallStatisticsService novaOverallStatisticsService;

    @Autowired
    private GatherSummaryService novaGatherSummaryService;

    @InitBinder("search")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("search.");
    }

    @RequestMapping(value = "socialBenefitsList.shtml")
    public ModelAndView loginList(HttpServletRequest request,
                                  ModelMap modelMap, String startDate, String endDate,
                                  Integer companycode) {
        log.info("社会效益开始："
                + DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
        if (StringUtil.isEmpty(startDate)) {
            // startDate = DateUtil.getAddDate("DATE", -7,
            // DateUtil.DEFAULT_DATE_FORMAT);
            startDate = ModelConstant.MIN_DATE;
        }
        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today();
        }
        // 获取客户的总计耗电量
        CustomerInfo customerInfo = novaCustomerInfoService.getByCompanyCode(companycode);
        Energy energy = novaGatherSummaryService.getEnergyByCompany(customerInfo.getId(),
                startDate, endDate);
        modelMap.put("energy", energy);
        Integer propertion = 0;
        try {
            if (customerInfo.getSavetarget() != null) {
                propertion = energy.getSavePower().divide(customerInfo.getSavetarget(), 2, BigDecimal.ROUND_CEILING).multiply(new BigDecimal(50)).intValue();
            } else {
                propertion = 0;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        modelMap.put("defaultStartDate", ModelConstant.MIN_DATE);
        modelMap.put("defaultEndDate", DateUtil.today());
        modelMap.put("propertion", propertion > 50 ? 50 : propertion);
        modelMap.put("customerInfo", customerInfo);
        modelMap.put("companycode", companycode);
        modelMap.put("startDate", startDate);
        modelMap.put("endDate", endDate);
        modelMap.put("customername",
                novaCustomerInfoService.getByCompanyCode(companycode)
                        .getCustomername());
        List<ElecEnergy> elecEnergies = novaElecEnergyService.findAll();
        modelMap.put("elecEnergies", elecEnergies);
        log.info("社会效益结束："
                + DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));

        return new ModelAndView("socialBenefits/socialBenefitsList", modelMap);
    }
}
